(1) Field of the Invention
The present invention relates to a program converting method for converting a program which is described in an object-oriented language, and more specifically to a program converting method for converting a virtual function call to a direct function call.
(2) Description of the Related Art
In recent years, with size increase of programs, in order to increase development efficiency of the programs and attempt to reuse the programs, or in order to improve serviceability of the programs, developments of programs described in object-oriented languages, such as C++ and JAVA™, have frequently been conducted.
One of characteristics of the object-oriented language is polymorphism. The polymorphism means characteristic in which an operation in the program is dynamically changed depending on a type of an instance of an object during execution of the program.
In order to realize the polymorphism, the C++ language, for example, has a feature which is a virtual function. The virtual function is described in detail below with reference to FIGS. 1 to 4.
FIG. 1 is a program that is described in the object-oriented C++ language. Keywords “virtual” which are designated by declarations of member functions f, g, and h in a class A designate that the member functions f, g, and h are virtual functions. The “virtual function” is a function whose execution is dynamically determined depending on a type of an object which is pointed by a pointer during the execution of the program, and in a case where a virtual function in a base class is re-defined in a derived class, when a pointer points an object in the derived class, a virtual function in the derived class is executed.
A class B inherits the class A, and a member function g in the class B is overridden on the virtual function g in the base class A and executes an operation which is unique to the class B.
Here, if the member function g is called by a pointer variable p in a function t2, the call becomes a virtual function call. Here, if an object which is pointed by the pointer variable p is an object in the class A, the function g in the class A is executed, and on the other hand, if the object is an object in the class B, the function g in the class B is executed. Thus, a function to be executed is determined depending on a type of an object which is pointed by the pointer variable p.
FIG. 2 shows a schematic diagram of a method for realizing the virtual function.
A class-B type object obj_b holds a pointer member vptr which points a virtual function table. The member vptr points a virtual function table vtbl_B of the class B. The virtual function table of the class B holds addresses of all virtual functions which can be called in the class B.
Here, it is assumed that the virtual function g is called by the pointer variable p.
In a case where the pointer variable p points the class-B type object obj_b, the p holds a first address of the obj13 b. An address of a function to be called by the virtual function call is necessary to be obtained in processing of firstly obtaining the virtual function table and then obtaining the address.
In other words, a value of the pointer variable p is added with an offset 1 to obtain a value of an address, and the obtained value is further added with an offset 2 to obtain a further value of the address, thereby obtaining the address of the function to be called.
Therefore, assembler codes for executing the virtual function call are as shown in FIG. 3. On the other hand, assembler codes for executing a direct call to a function are as shown in FIG. 4. As shown in the figures, the codes for the virtual function call tends to have more number of orders and a lower execution speed compared to the codes for the direct function call.
Therefore, the more the number of the virtual function calls is decreased, the more execution performance of the program is improved.
As conventional technologies for reducing the number of executions of the virtual function calls and improving the execution performance, there are technologies in which an address of a function to be called by the virtual function call is compared with an address of a specific function, and if the function to be called is the specific function, the function is directly called (Japanese Patent Laid-Open No. 2000-40007 publication, for example). Thereby, it is possible to convert the codes selectively by executing codes for directly calling the function when the function to be executed by the virtual function call can be specified, and executing codes for the general virtual function call when the function cannot be specified. FIG. 6 shows a schematic diagram in which a function h indicated by a directional arrow (A) in the program shown in FIG. 5 is converted by a method described in the Japanese Patent Laid-Open No. 2000-40007 publication. Thus, in a case where an address of a virtual function is an address of a member function “A::f” (function f defined in the class A), the function “A::f” is directly called, and in other cases, the virtual function call is performed. Furthermore, by performing inline expansion of the direct call to functions, it is possible to improve the execution performance of the program.
There are further technologies in which inheritance relationship among classes are analyzed, and a virtual function call in classes which do not have any derived classes is converted into a direct function call (Japanese Patent Laid-Open No. 10-320204 publication, for example). Thereby, the virtual function call can be converted into the direct function call during compiling the program, so that it is possible to improve the execution performance of the program. FIG. 7 shows a schematic diagram in which the function h indicated by the directional arrow (A) in the program shown in FIG. is converted by the method described in the Japanese Patent Laid-Open No. 10-320204 publication.
Here, if a class from which a virtual function is called can be specified, it is possible to specify a function to be called by the virtual function call. For example, there is a case that it is possible to specify that the member function f in the class A indicated by a directional arrow (B) of FIG. 5 has only class-A type pointer “this”. In such a case, when the virtual function g is called by the pointer “this”, a function to be called is always the member function g in the class A. Note that the pointer “this” means a special pointer for pointing, in the member function, an object whose member function is invoked.
However, in the technology described in the Japanese Patent Laid-Open No. 2000-40007 publication, as shown in FIG. 8, even if the class from which the virtual function is called can be specified, there is a problem that the execution performance and a code size of the program are reduced compared to the codes for the direct function call, due to generation of the code for comparing the address of the function.
Further, since a type of the pointer “this” is the class A, in the technology described in the Japanese Patent Laid-Open No. 10-320204 publication, in a case where the class from which the virtual function is called has a derived class, there is a problem that the virtual function call cannot be converted into the direct function call.